package com.unohacha.qqj.fzdr.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qpg.superhttp.SuperHttp;
import com.qpg.superhttp.callback.SimpleCallBack;
import com.qpg.widget.common.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.adapter.MyImgLibListAdapter;
import com.unohacha.qqj.fzdr.adapter.SysMessageAdapter;
import com.unohacha.qqj.fzdr.adapter.SysPushAdapter;
import com.unohacha.qqj.fzdr.bean.BaseBean;
import com.unohacha.qqj.fzdr.bean.BaseListBean;
import com.unohacha.qqj.fzdr.bean.MyImgListBean;
import com.unohacha.qqj.fzdr.bean.SysMessage;
import com.unohacha.qqj.fzdr.bean.SysPuthBean;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SysMessageActivity extends BaseActivity {
    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    @BindView(R.id.common_right_text)
    TextView commonRightText;
    @BindView(R.id.car_list_recycler)
    RecyclerView carListRecycler;

    SysMessageAdapter sysMessageAdapter;
    private int mNextRequestPage = 1;
    private static final int PAGE_SIZE = 10;
    private String typeid="1";
    private String id="";
    List<SysMessage> listBeans;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sys_message;
    }

    @Override
    protected void initData() {
        super.initData();
        commonTitle.setText("系统消息");
        commonRightImage.setVisibility(View.GONE);
        commonRightText.setVisibility(View.VISIBLE);
        commonRightText.setTextColor(getResources().getColor(R.color.text_color_green));
//        commonRightText.setPadding(0,5,10,5);
//        commonRightText.setWidth(70);
//        commonRightText.setTag(0);
        commonRightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if((int)commonRightText.getTag()==0){
                    commonRightText.setText("完成");
                    commonRightText.setTag(1);
                    sysMessageAdapter.setIsShowImg(true);
                    sysMessageAdapter.notifyDataSetChanged();

                }else {
                    commonRightText.setText("编辑");
                    commonRightText.setTag(0);
                    sysMessageAdapter.setIsShowImg(false);
                    sysMessageAdapter.notifyDataSetChanged();

                    StringBuilder stringBuilder=new StringBuilder();
                    if(listBeans==null){
                        return;
                    }
                    for (int i=0;i<listBeans.size();i++){
                        if (listBeans.get(i).isChecked()){
                            //记录id
                            stringBuilder.append(listBeans.get(i).getId()+",");
                            listBeans.remove(i);
                        }
                    }
                    if(!stringBuilder.toString().contains(",")){
                        return;
                    }
                    SuperHttp.post("api/mymessage/systemMessageDel")
                            .addParam("token",Prefer.getInstance().getToken())
                            .addParam("id",stringBuilder.toString())
                            .request(new SimpleCallBack<BaseBean<Object>>() {
                                @Override
                                public void onSuccess(BaseBean<Object> data) {

                                    ToastUtil.showCustomToast(data.getMessage());
                                }

                                @Override
                                public void onFail(int errCode, String errMsg) {

                                }
                            });
                }
            }
        });
        commonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        listBeans=new ArrayList<>();
        sysMessageAdapter=new SysMessageAdapter(listBeans);
        carListRecycler.setLayoutManager(new LinearLayoutManager(this, LinearLayout.VERTICAL,false));

        carListRecycler.setAdapter(sysMessageAdapter);
        sysMessageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                Intent intent=new Intent(SysMessageActivity.this,SysMessageDetialActivity.class);
                startActivity(intent);
            }
        });
        getData();
    }

    private void getData(){
        String token = Prefer.getInstance().getToken();
        SuperHttp.post("api/mymessage/systemDetailMessage")
                .addParam("token",token)
                .request(new SimpleCallBack<BaseBean<BaseListBean<SysMessage>>>() {
                    @Override
                    public void onSuccess(BaseBean<BaseListBean<SysMessage>> data) {

                        if(data.getCode()==1){
                            listBeans=data.getData().getData();
                            setData(true,data.getData().getData());
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });
    }

    private void setData(boolean isRefresh,List<SysMessage> progect) {

        mNextRequestPage++;
        if (progect == null || progect.size() == 0) {
            return;
        }
        final int size = progect == null ? 0 : progect.size();
        if (isRefresh) {
            sysMessageAdapter.setNewData(progect);
        } else {
            if (size > 0) {
                sysMessageAdapter.addData(progect);
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            sysMessageAdapter.loadMoreEnd(isRefresh);
            //   Toast.makeText(this, "no more data", Toast.LENGTH_SHORT).show();
        } else {
            sysMessageAdapter.loadMoreComplete();
        }
    }

    public void isChecklist(int position){
        if (listBeans.get(position).isChecked()){
            listBeans.get(position).setChecked(false);
        }else{
            listBeans.get(position).setChecked(true);
        }
        sysMessageAdapter.notifyDataSetChanged();
    }
}
